package cn.sharp.bbs.service.impl;

import cn.sharp.bbs.common.Constants;
import cn.sharp.bbs.controller.msg.MsgType;
import cn.sharp.bbs.dao.gen.CommentMapper;
import cn.sharp.bbs.dao.gen.UserMapper;
import cn.sharp.bbs.dao.msg.MsgDao;
import cn.sharp.bbs.entity.BO.CurrentUser;
import cn.sharp.bbs.entity.DO.msgDto;
import cn.sharp.bbs.service.MsgService;
import cn.sharp.bbs.service.SSEService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class MsgServiceImpl implements MsgService {

    @Resource
    private MsgDao msgDao;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private SSEService sseService;

    @Override
    public void read(int type, Long u_id) {
        switch (type) {
            case 1: //点赞
                msgDao.read(u_id, MsgType.POST_LIKE);
                msgDao.read(u_id, MsgType.COMMENT_LIKE);
                break;
            case 5: //收藏
                msgDao.read(u_id, MsgType.COLLECT);
                break;
            case 2: //评论
                msgDao.read(u_id, MsgType.COMMENT);
                msgDao.read(u_id, MsgType.COMMENT_FOLLOW);
                msgDao.read(u_id, MsgType.REPLY_FOLLOW);
                break;
            case 3: //回复
                msgDao.read(u_id, MsgType.REPLY);
                break;
            case 6: //关注
                msgDao.read(u_id, MsgType.FOLLOW);
                break;
            case 8: //举报
                msgDao.read(u_id, MsgType.REPORT_SUCCESS);
                msgDao.read(u_id, MsgType.REPORT_FAIL);
                msgDao.read(u_id, MsgType.REPORTED);
                break;
            case 7: //帖子审核
                msgDao.read(u_id, MsgType.POST_CHECK);
                break;
        }
        sseService.push(u_id, MsgType.READ);
    }

    @Override
    public void readLetter(Long chatRoom, Long u_id) {
        msgDao.readLetter(u_id, chatRoom, MsgType.LETTER);
        sseService.push(u_id, MsgType.READ);
    }

    @Override
    public int unread(int type, Long u_id) {
        int num = 0;
        switch (type) {
            case 1: //点赞
                num += msgDao.getUnreadNum(u_id, MsgType.POST_LIKE);
                num += msgDao.getUnreadNum(u_id, MsgType.COMMENT_LIKE);
                break;
            case 5: //收藏
                num += msgDao.getUnreadNum(u_id, MsgType.COLLECT);
                break;
            case 2: //评论
                num += msgDao.getUnreadNum(u_id, MsgType.COMMENT);
                num += msgDao.getUnreadNum(u_id, MsgType.COMMENT_FOLLOW);
                num += msgDao.getUnreadNum(u_id, MsgType.REPLY_FOLLOW);
                break;
            case 3: //回复
                num += msgDao.getUnreadNum(u_id, MsgType.REPLY);
                break;
            case 6: //关注
                num += msgDao.getUnreadNum(u_id, MsgType.FOLLOW);
                break;
            case 8: //举报
                num += msgDao.getUnreadNum(u_id, MsgType.REPORT_SUCCESS);
                num += msgDao.getUnreadNum(u_id, MsgType.REPORT_FAIL);
                num += msgDao.getUnreadNum(u_id, MsgType.REPORTED);
                break;
            case 7: //帖子审核
                num += msgDao.getUnreadNum(u_id, MsgType.POST_CHECK);
                break;
            case 4: //私信
                num += msgDao.getUnreadNum(u_id, MsgType.LETTER);
                break;
        }
        return num;
    }

    @Override
    public int unreadLetter(Long chatRoom, Long userId) {
        return msgDao.getUnreadLetterNum(userId, chatRoom, MsgType.LETTER);
    }

    private void addAvatarPath(List<msgDto> list) {
        for (msgDto m: list) {
            m.setUserAvatar(Constants.AVATAR_URL + m.getUserAvatar());
        }
    }

    @Override
    public List<msgDto> likeMsg(Long u_id) {
        List<msgDto> list = new ArrayList<>();
        List<msgDto> list1 = msgDao.likePostMsg(u_id); //1
        List<msgDto> list2 = msgDao.likeCommentMsg(u_id);//2
        for (msgDto i : list1) {
            i.setTargetName(msgDao.getPostName(i.getTargetId()));
        }
        list.addAll(list1);
        list.addAll(list2);
        Collections.sort(list);
        addAvatarPath(list);
        return list;
    }

    @Override
    public List<msgDto> collectMsg(Long u_id) {
        List<msgDto> list = msgDao.collectMsg(u_id); //3
        for (msgDto i : list) {
            i.setTargetName(msgDao.getPostName(i.getTargetId()));
        }
        return list;
    }

    @Override
    public List<msgDto> commentMsg(Long u_id) {
        List<msgDto> list = new ArrayList<>();
        List<msgDto> list1 = msgDao.postCommentMsg(4, u_id); //4
        List<msgDto> list2 = msgDao.postCommentMsg(5, u_id); //5
        List<msgDto> list3 = msgDao.postCommentMsg(6, u_id); //6
        list.addAll(list1);
        list.addAll(list2);
        list.addAll(list3);
        Collections.sort(list);
        for (msgDto i : list) {
            i.setTargetName(msgDao.getPostName(i.getTargetId()));
        }
        addAvatarPath(list);
        return list;
    }

    @Override
    public List<msgDto> replyMsg(Long u_id) {
        List<msgDto> list = msgDao.replyMsg(u_id);
        addAvatarPath(list);
        return  list; //7
    }

    @Override
    public List<msgDto> followMsg(Long u_id) {
        List<msgDto> list = msgDao.followMsg(u_id); //8
        addAvatarPath(list);
        return list;
    }

    @Override
    public List<msgDto> reportMsg(Long u_id) {
        List<msgDto> list = msgDao.reportMsg(u_id);//9, 10, 11
        for (msgDto i : list) {
            if (i.getUserId() == 0L) { //post
                i.setTargetName(msgDao.getPostName(i.getTargetId()));
            } else if (i.getUserId() == 1L) { //comment
                i.setTargetName(commentMapper.selectByPrimaryKey(i.getTargetId()).getCont());
            } else if (i.getUserId() == 2L) { //user
                i.setTargetName(userMapper.selectByPrimaryKey(i.getTargetId()).getName());
            }
        }
        Collections.sort(list);
        addAvatarPath(list);
        return list;
    }

    @Override
    public List<msgDto> postCheckMsg(Long u_id) {
        return msgDao.postCheckMsg(u_id); //12
    }


}
